package zhcms.admin;

import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import zhcms.db.DBManager;
import zhcms.db.DbConn;
import zhcms.db.ConnectionPool.PooledConnection;
import zhcms.framework.Action;
import zhcms.framework.ActionForward;
import zhcms.tool.GyPage;


public class GyFriendLink implements Action{
	String pageStr="";
	
	public GyFriendLink()
	{
		
	}
	@Override
	public ActionForward excute(HttpServletRequest request,
			HttpServletResponse response) {
		return null;
	}
	
	public int listcount(){
		ResultSet rs = null;
		PooledConnection conn = null;
		String sql="select count(*) as id from gyhy_friendlink";
		int countlyb=0;
		try {
			conn = DBManager.getConnection();
			rs = conn.executeQuery(sql);
			if (rs.next())
			{
				countlyb=rs.getInt("id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
            conn.close();
		}
		return countlyb;
	}
	
	public ArrayList<HashMap<String, String>> getAllList(int currentpage)
	{
		int listnumcount=this.listcount();
		GyPage gyp=new GyPage();
		gyp.setP(15, listnumcount, "", "", currentpage);
		String limitStr=gyp.limit();
		pageStr=gyp.outPut();
		String sql="select Id,LinkName,LinkImgPath,LinkURL,if(istype=1 ,'文字','图片'),OrderNum,LinkInfo from gyhy_friendlink order by Id desc limit "+limitStr;
		return DbConn.executeQuery(sql);
	}
	
	public ArrayList<HashMap<String, String>> getOneList(int postid)
	{
		String sql="select Id,LinkName,LinkImgPath,LinkURL,if(istype=1 ,'文字','图片'),OrderNum,LinkInfo from gyhy_friendlink where Id='"+postid+"'";
		return DbConn.executeQuery(sql);
	}
	
	public ArrayList<HashMap<String, String>> getOneList(String postid)
	{
		String sql="select Id,LinkName,LinkImgPath,LinkURL,if(istype=1 ,'文字','图片'),OrderNum,LinkInfo from gyhy_friendlink where Id='"+postid+"'";
		return DbConn.executeQuery(sql);
	}
	
	public String getPageStr() {
		return pageStr;
	}

	public void setPageStr(String pageStr) {
		this.pageStr = pageStr;
	}
	
	public ActionForward save(HttpServletRequest request, HttpServletResponse response)
	{
		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("utf-8");
		} 
		catch (UnsupportedEncodingException e1) 
		{
			
		}
		String postid=getV(request.getParameter("postid"));
		//System.out.println("postid+++++++++++++"+postid);
		String LinkName=getV(request.getParameter("LinkName"));
		
		String LinkImgPath=getV(request.getParameter("ImgPath"));
		
		String LinkURL=getV(request.getParameter("LinkURL"));
		
		String LinkInfo=getV(request.getParameter("LinkInfo"));
		
		String istype=getV(request.getParameter("istype"));
		
		String OrderNum=getV(request.getParameter("OrderNum"));
		
		PooledConnection conn = null;
		String sqlStr1="";
		
		if(postid==null || postid.length()<=0 || postid.equals("null"))
		{
			sqlStr1="insert into `gyhy_friendlink` ( `LinkName` , `LinkURL` , `LinkImgPath` , `LinkInfo` , `istype` , `OrderNum`) values('"+LinkName+"','"+LinkURL+"','"+LinkImgPath+"','"+LinkInfo+"','"+istype+"','"+OrderNum+"');";
		}
		else
		{
			sqlStr1="update `gyhy_friendlink` set `LinkName`='"+LinkName+"' , `LinkURL`='"+LinkURL+"' , `LinkImgPath`='"+LinkImgPath+"' , `LinkInfo`='"+LinkInfo+"' , `istype`='"+istype+"' , `OrderNum`='"+OrderNum+"' where `id`="+postid;
		}
		
		//System.out.println(sqlStr1);
		boolean isok=false;
		try {
			conn = DBManager.getConnection();
			conn.executeUpdate(sqlStr1);
			conn.close();
			isok=true;
		} catch (SQLException e) {
			isok=false;
			e.printStackTrace();
		}
		finally {
            conn.close();
		}
		
		if(isok==true)
		{
			return new ActionForward("/admin/admin_friend.jsp",3,"更新成功");
		}
		else
		{
			return new ActionForward("/admin/admin_friend.jsp",3,"更新失败");
		}
	}
	
	public ActionForward del(HttpServletRequest request, HttpServletResponse response)
	{
		
		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("utf-8");
		} 
		catch (UnsupportedEncodingException e1) 
		{
			
		}
		String postid=getV(request.getParameter("postid"));
		//System.out.println("postid+++++++++++++"+postid);
		
		PooledConnection conn = null;
		String sqlStr1="";
		
		sqlStr1="delete from  `gyhy_friendlink` where `id`="+postid;
		
		//System.out.println(sqlStr1);
		boolean isok=false;
		try {
			conn = DBManager.getConnection();
			conn.executeUpdate(sqlStr1);
			conn.close();
			isok=true;
		} catch (SQLException e) {
			isok=false;
			e.printStackTrace();
		}
		finally {
            conn.close();
		}
		if(isok==true)
		{
			return new ActionForward("/admin/admin_friend.jsp",3,"更新成功");
		}
		else
		{
			return new ActionForward("/admin/admin_friend.jsp",3,"更新失败");
		}
	}

	public static String TransactSQLInjection(String str)
	{
	    return str.replaceAll(".*([';]+|(--)+).*", " ");
	}

	private String getV(String S)
	{
		//System.out.println(S);
		String returnS;
		if(S==null || S.length()<=0)
		{
			returnS="";
		}
		else
		{
			returnS=S;
		}
		return TransactSQLInjection(returnS);
	}
	
}
